# encoding: utf-8
from opsplatform.api import *
from django.contrib.auth.decorators import login_required
from models import Message, STATUS, UserMessage


@require_permission('account.perm_can_view_message')
def message_list(request):
    header_title, path1, path2 = '消息列表', '收件箱', '消息列表'
    message_list = UserMessage.objects.filter(user=request.user.username).order_by('-id')
    message_id = request.GET.get('id', '')
    status_all = list(STATUS)
    env_value = request.GET.get('env', '')
    status_value = request.GET.get('status', '')
    if status_value:
        message_list = message_list.filter(status=status_value)

    message_list, p, messages, page_range, current_page, show_first, show_end = pages(message_list, request)
    return render_to_response('message/message_list.html', locals(), context_instance=RequestContext(request))


@require_permission('account.perm_can_view_message')
def message_detail(request):
    header_title, path1, path2 = '查看消息', '收件箱', '查看消息'
    message_id = request.GET.get('id', '')
    usermessage_id = request.GET.get('usermessage_id', '')
    if not message_id:
        return HttpResponseRedirect(reverse('message_list'))
    message = get_object(Message, id=message_id)
    usermessage = UserMessage.objects.get(id=usermessage_id)
    if usermessage:
        usermessage.status = '2'
        usermessage.save()
    if not message:
        return HttpResponseRedirect(reverse('message_list'))
    return render_to_response('message/message_detail.html', locals(), context_instance=RequestContext(request))


def message_read(request):
    message_ids = request.GET.get('id', '')
    message_id_list = message_ids.split(',')
    for message_id in message_id_list:
        UserMessage.objects.filter(id=message_id).update(status='2')

    return HttpResponse('已读成功')


def message_del(request):
    message_ids = request.GET.get('id', '')
    message_id_list = message_ids.split(',')
    for message_id in message_id_list:
        UserMessage.objects.filter(id=message_id).delete()

    return HttpResponse('删除成功')


@csrf_exempt
def message_create(request):
    if request.method == 'POST':
        try:
            data = json.loads(request.body)
            data = data.get('data')
            Message.objects.get_or_create(id=data.get('id'),
                                          subject=data.get('subject'),
                                          text=data.get('text'),)
        except Exception, e:
            print e
            return JsonResponse({'msg': "parameter format invalid.", 'code': 0})
    else:
        return JsonResponse({'msg': "not POST method.", 'code': 0})
    return JsonResponse({'msg': 'success', 'code': 1})


@csrf_exempt
def usermessage_create(request):
    if request.method == 'POST':
        try:
            data = json.loads(request.body)
            data = data.get('data')
            message = Message.objects.get(id=data.get('message'))
            UserMessage.objects.get_or_create(id=data.get('id'),
                                              user=data.get('user'),
                                              message=message,
                                              status=data.get('status'),
                                              showed=data.get('showed'))
        except Exception, e:
            print e
            return JsonResponse({'msg': "parameter format invalid.", 'code': 0})
    else:
        return JsonResponse({'msg': "not POST method.", 'code': 0})
    return JsonResponse({'msg': 'success', 'code': 1})
